package com.itboye.ihomebank.key;

import java.util.Observable;
import java.util.Observer;

import org.greenrobot.greendao.database.Database;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.DownKeyBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.ttKey.DaoMaster;
import com.itboye.ihomebank.ttKey.DaoSession;
import com.itboye.ihomebank.ttKey.DbService;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.ttKey.KeyDao;
import com.itboye.ihomebank.ttKey.MyPreference;
import com.itboye.ihomebank.ttKey.Operation;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
/**
 *普通用户
 *
 */
public class ActivityPuTongKey extends BaseOtherActivity implements Observer{
	ImageView putongkey_finish;//返回
	TextView putongkey_title;//标题
	
	ImageView putong_kaisuo;
	
	private String lockName;
	private String lockMac;;
	private String keyId;
	public static String lockid;
	
	public static boolean addLock = false;
	
	UserPresenter userPresenter;
	String uid;
	public static KeyDao keyDao;
	private static DaoSession daoSession;
	
	DownKeyBean bean;
	Gson gson = new Gson();
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.putongkey_finish:
			finish();
			break;
			
		case R.id.putong_kaisuo:
			KeyFragment.bleSession.setOperation(Operation.UNLOCK);
			KeyFragment.bleSession.setLockmac(lockMac);
			addLock = true;
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_puong_key;
	}

	@Override
	protected void onInitController() {
		DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(
				this, "zhujia.db", null);
		Database db = helper.getWritableDb();
		daoSession = new DaoMaster(db).newSession();
		keyDao = daoSession.getKeyDao();
	}

	@Override
	protected void onInit() {
		lockName = getIntent().getStringExtra("lockName");
		lockMac = getIntent().getStringExtra("lockMac");
		keyId= getIntent().getStringExtra("keyid");
		lockid= getIntent().getStringExtra("lockid");
		
		putongkey_title.setText(lockName);
		
		userPresenter = new UserPresenter(this);
		
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		
		//判断钥匙是否本地存在，存在则无需下载
		String accessToken = MyPreference.getStr(mContext,
				MyPreference.ACCESS_TOKEN);
		Key localKey = DbService.getKeyByAccessTokenAndLockmac(accessToken,
				lockMac);
		if(localKey!=null){
			ByAlert.alert("钥匙已存在，无需下载");
		}else{
			showProgressDialog("下载钥匙,请稍后", true);
			userPresenter.downLoadKey(uid, keyId, lockid);
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.down_success) {
				ByAlert.alert(entity.getData());
				bean=(DownKeyBean) entity.getData();
				
				final Key key = new Key();
				key.setAccessToken(MyPreference.getStr(mContext,
						MyPreference.ACCESS_TOKEN));
				key.setAdmin(false);
				key.setLockVersion(gson.toJson(bean.getLockVersion()));
				key.setStartDate(bean.getStartDate());
				key.setEndDate(bean.getEndDate());
				key.setUnlockKey(bean.getLockKey());
				key.setLockFlagPos(bean.getLockFlagPos());
				key.setLockName(bean.getLockName());
				key.setLockMac(bean.getLockMac());
				key.setTimestamp(0);
				key.setAesKeystr(bean.getAesKeyStr());
				key.setId(keyDao.insert(key));
				
			}	
			if (entity.getEventType() == UserPresenter.down_fail) {
				ByAlert.alert(entity.getData());
				
			}	
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
	}

}
